<template>
  <div class="site-wrapper">
    <div class="header clearfix">
      <!-- <el-button size="small" type="primary" @click="openEmailDialog(false)" v-if="haslimit">新增</el-button> -->
      <div class="filter">
        <el-input v-model="filterData.subject" placeholder="请输入主题" prefix-icon="el-icon-search" @input="searchItem(true)"></el-input>
      </div>
    </div>
    <div class="data-wrapper">
      <el-table :height="tableHeight" :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="index" label="序号" width="55"></el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" prop="subject" label="主题" min-width="180"></el-table-column>
        <el-table-column align="center" prop="createdAt" label="创建日期" width="100">
          <template slot-scope="scope">{{!scope.row.createdAt ? '-' : scope.row.createdAt.substring(0, 10)}}</template>
        </el-table-column>
        <el-table-column align="center" prop="updatedAt" label="更新日期" width="100">
          <template slot-scope="scope">{{!scope.row.updatedAt ? '-' : scope.row.updatedAt.substring(0, 10)}}</template>
        </el-table-column>
        <el-table-column label="操作" min-width="100" align="center" v-if="haslimit">
          <template slot-scope="scope">
            <el-button type="text" size="small" style="padding: 0;" @click="openEmailDialog(scope.row)">编辑</el-button>
            <el-button @click="delMessage(scope.row.id)" type="text" size="small" style="padding: 0;">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style=""
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="page"
        :page-sizes="[15, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <Email ref="email" @refresh="searchItem(true)" />
  </div>
</template>

<script>
import dingMixin from "@/mixins/ding";
import formUtil from "@/utils/formUtil";
import Email from './Email'

export default {
  data() {
    return {
      showFilter: false,
      loading:false,
      showForm: false,
      tableHeight: window.tableHeight,
      tableData: [],
      filterData: {
        subject: '',
      },
      emailForm: {
        account: '',
        password: '',
        nickname: '',
        servicePath: '',
        sendServicePath: ''
      },
      isCheck: false,
      page:1,
      pageSize: 15,
      totalPage: 1,
    }
  },
  mixins: [dingMixin],
  components: {
    Email
  },
  computed: {
    haslimit() {
      return this.userInfo.roleIdList && (this.userInfo.roleIdList.includes('1311210362356432897') || this.userInfo.roleIdList.includes('1328502655740379138'))
    },
  },
  async mounted() {
    this.$nextTick(() => {
      this.tableHeight = document.documentElement['clientHeight'] - 170;
    })
    window.onresize = () => {
      this.tableHeight = document.documentElement['clientHeight'] - 170
    }
    this.searchItem(true);
  },
  methods: {
    openEmailDialog(data) {
      this.$refs.email.openEmail(data);
    },
    delMessage(id) {
      formUtil.submitConfirm('您确定要删除草稿吗？', async ()=> {
        let {data: res} = await this.$http.post('bms/mail/message/delete', {id: id})
        if (res.resultCode !== "000") return this.$message.error(res.resultMessage);
        formUtil.submitSuccess('删除成功');
        this.searchItem(true);
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    sizeChangeHandle(limit) {
      this.pageSize = limit;
      this.searchItem(true);
    },
    currentChangeHandle(page) {
      this.page = page;
      this.searchItem();
    },
    async searchItem(init) {
      if(init) this.page = 1;
      let {data: res} = await this.$http.post('bms/mail/message/list?pageNum='+this.page+'&pageSize='+this.pageSize,{ subject: this.filterData.subject })
      if (res.resultCode !== "000") return this.$message.error(res.resultMessage);
      this.tableData = res.data.list
      this.totalPage = Number(res.data.total)
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    position: relative;
    padding: 10px 0 0 20px ;
    .filter{
      cursor: pointer;
      float: right;
      margin-right: 30px;
      line-height: 30px;
      img{
        width: 16px;
        margin-right: 3px;
      }
    }
    .mask{
      position: fixed;
      height: 100vh;
      width: 100%;
      top: 0;
      right: 0;
      z-index: 3;
    }
    .filter-wrapper{
      position: absolute;
      width: 455px;
      padding-bottom: 20px;
      background: white;
      top: 48px;
      right: 20px;
      z-index: 99;
      border-radius: 5px;
      box-shadow: 0 0 3px black;
      .filter-header{
        line-height: 40px;
        padding-left: 16px;
        border-bottom: 1px solid #DDD;
        margin-bottom: 20px;
      }
      .filter-arrow{
        width:12px;
        height:12px;
        position: absolute;
        top: -7px;
        right: 20px;
        box-shadow: 0 0 3px black;
        transform: rotate(45deg);
      }
      .filter-white{
        width:18px;
        height:18px;
        position: absolute;
        top: -2px;
        right: 17px;
        border-radius: 3px;
        background: white;
      }
    }
  }
  .data-wrapper{
    padding: 0 20px;
    margin-top: 10px;
  }
  .select-btn{
    display: inline-block;
    font-weight: normal;
    margin-right: 5px;
    width: 80px;
    height: 28px;
    border-radius: 30px;
    background: #FAFAFA;
    border: 1px solid #ECECEC;
    position: relative;
    line-height: 28px;
    color: #7982A7;
    font-size: 12px;
    text-align: center;
    position: relative;
    cursor: pointer;
    .del{
      position: absolute;
      top:0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 30px;
      color: white;
      text-align: center;
      background:rgba(130, 130, 130, 0.9);
      display: none;
    }
    &:hover{
      .del{
        display: block;
      }
    }
  }
  .fileitem{
    line-height: 32px;
    margin-bottom: 5px;
    background: #F5F5F5;
    width: 474px;
    padding-left:15px;
    font-size: 12px;
    .uploadtip{
      float:right;
      margin-right: 20px;
      cursor: pointer;
    }
  }
  .addfilebtn{
    cursor: pointer;
    color: #909399;
    display: block;
    font-weight: normal;
  }
</style>